package com.myCommunity.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.myCinema.dao.HibernateUtil;
import com.myCommunity.dao.MessageSendingDao;
import com.myCommunity.entity.MessageSending;

public class MessageSendingDaoImpl implements MessageSendingDao {

	public List<MessageSending> getListByReceiverId(int receiverId, int readed) {
		Session sess = null;
		try{
			sess = HibernateUtil.openSession();
			String hql = "from MessageSending m inner join MessageReceiving r where r.receiverId = :receiverId and 1=1";
			if(readed > 0 )
				hql += " and r.readed = :readed";
			Query q = sess.createQuery(hql);
			q.setInteger("receiverId", receiverId);
			q.setInteger("readed", readed);
			return q.list();
		}finally{
			sess.close();
		}
	}

	public static void main(String[] args) {
		List<MessageSending> list = new MessageSendingDaoImpl().getListByReceiverId(1, 0);
		for(MessageSending m : list ){
			System.out.println(m.getContent());
		}
	}

}
